/*
 * Created on Mar 2, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.tree.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.*;
import java.io.*;

import ModuleBuilder.model.*;
import ModuleBuilder.UI.*;
import ModuleBuilder.UI.tree.*;
/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Loader {
	static String[] scenarios = new String[] {".scn"};
	
	public static void load( BuilderManager manager, JFrame parent ) {
		ScenarioModel scenarioMod=null;
		
		//Show file chooser to allow the user to get the file
		JFileChooser fc = Storer.getFc();
		fc.resetChoosableFileFilters();
		fc.addChoosableFileFilter(new SimpleFileFilter(scenarios, "Scenario Builder Scenario files"));
		int returnVal = fc.showDialog(parent, "Select Scenario File Name To Load");
		File file=null;
		
		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			String fn = file.getPath();


		} else {
			Debug.out("Select Scenario File cancelled by user.");
			return;
		}
		// now serializr the result
		FileInputStream fi=null;
		BufferedInputStream bi=null;
		ObjectInputStream oi = null;
		try {
			fi = new FileInputStream(file);
			bi = new BufferedInputStream(fi);
			oi = new ObjectInputStream(bi);
			scenarioMod = (ScenarioModel)oi.readObject();
			
			oi.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
			return;
		}
		// now we have the ScenarioModel in memory, populate the manager
		//
		setupGameState(scenarioMod, manager );
	}
	
	private static void setupGameState(ScenarioModel scenarioMod, BuilderManager manager ) {
		// get the model from file
		PictureCollectionModel topPixMod = scenarioMod.getTopFigurePix();
		PictureCollectionModel sidePixMod = scenarioMod.getSideFigurePix();
		PictureCollectionModel terrainPixMod = scenarioMod.getMapTilePix();
		GameParameterModel gameParam = scenarioMod.getGameParamModel();
		MapModel mapMod = scenarioMod.getMapModel();		
		DefaultMutableTreeNode rootNode = scenarioMod.getTreeRoot();
		
		// get the individual editors
		PictureBuilder topFigBuilder = manager.getFigureTopBuilder();
		PictureBuilder sideFigBuilder = manager.getFigureSideBuilder();
		PictureBuilder terrainTileBuilder = manager.getMapTileBuilder();
		GameParameter gameParameter = manager.getGameParameter();
		MapBuilder mapBuilder = manager.getMapBuilder();
		GameTreeEditor treeEditor = manager.getGameTreeEditor();

		// set model ==> editor
		topFigBuilder.load(topPixMod);
		sideFigBuilder.load(sidePixMod);
		terrainTileBuilder.load(terrainPixMod);
		gameParameter.load(gameParam);
		mapBuilder.load(mapMod);
		treeEditor.load(rootNode);
		return;
	}
	
}
